package client

import (
	"runtime"
	"time"

	"github.com/lxxtec/lwRPC/codes"
	"github.com/lxxtec/lwRPC/cryptology"
	"github.com/lxxtec/lwRPC/discovery"
	"github.com/lxxtec/lwRPC/load_balance"
)

// type connectPool struct {
// 	connect *Connect
// 	pool    chan LightClient
// 	mu      sync.Mutex
// }

type Options struct {
	Discovery         discovery.Descovery
	loadBalancing     load_balance.LoadBalancing
	serializationType codes.SerializationType
	compressorType    codes.CompressorType

	pool         int
	cryptology   cryptology.Cryptology
	rsaPublicKey []byte
	writeTimeout time.Duration
	readTimeout  time.Duration
	heartBeat    time.Duration
	Trace        bool
	AUTH         string
}

func defaultOptions() *Options {
	defaultPoolSize := runtime.NumCPU() * 4
	if defaultPoolSize < 20 {
		defaultPoolSize = 20
	}
	return &Options{
		pool:              defaultPoolSize,
		serializationType: codes.MsgPack,
		compressorType:    codes.Snappy,
		loadBalancing:     load_balance.NewPolling(),
		cryptology:        cryptology.AES,
		rsaPublicKey: []byte(`
		-----BEGIN PUBLIC KEY-----
		MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWviNW8C1f+cjy8KF0qT93AA1q
		lbQTXPKO4qm34bf6UnSpXgemm1zTEgcPu5Ifka2GgTEgeUMD//iwxr3BTNYA0ARc
		soVSN53vklXJqRL3xMWNUFg/2bsAZn5Irlw1xRZfzFzqyCDk5JvUCejvHjvjQwOH
		YGHsCfV0pvxPlwFq4wIDAQAB
		-----END PUBLIC KEY-----`),
		writeTimeout: time.Minute,
		readTimeout:  time.Minute * 3,
		heartBeat:    time.Minute,
		Trace:        false,
		AUTH:         "",
	}
}

type Option func(options *Options)

func SetPoolSize(p int) Option {
	if p <= 0 {
		p = 25
	}
	return func(options *Options) {
		options.pool = p
	}
}
